#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
#include <QTimeEdit>
#include <QMap>
#include <QDialog>
#include <QListWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPainter>
#include <QVector>
#include <QTableWidget>
#include <QColorDialog>
#include <QSpinBox>
#include <QSlider>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QUrl>
#include<QLabel>
namespace Ui
{
class MainWindow;
}

struct PlanItem
{
    QString name;
    int duration;
    int remaining;
    QColor color;
};

class FlowerSelectionDialog : public QDialog
{
    Q_OBJECT

public:
    explicit FlowerSelectionDialog(QWidget* parent = nullptr);
    QString selectedFlower() const;

private:
    QListWidget* flowerList;
};

class MusicSelectionDialog : public QDialog
{
    Q_OBJECT

public:
    explicit MusicSelectionDialog(QWidget* parent = nullptr);
    QString selectedMusic() const;

private:
    QListWidget* musicList;
};

class PlanInputDialog : public QDialog
{
    Q_OBJECT

public:
    explicit PlanInputDialog(QWidget* parent = nullptr);
    QVector<PlanItem> getPlanItems() ;

private slots:
    void addPlanItem();
    void removePlanItem();

private:
    QTableWidget* planTable;
    QPushButton* addButton;
    QPushButton* removeButton;
};

class PieChartWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PieChartWidget(const QVector<PlanItem>& items, QWidget* parent = nullptr);
    void updateItems(const QVector<PlanItem>& newItems);
    void paintEvent(QPaintEvent* event) override;

private:
    QVector<PlanItem> items;
    int totalDuration;
};

class GardenItem : public QLabel
{
    Q_OBJECT

public:
    explicit GardenItem(const QPixmap& pixmap, QWidget* parent = nullptr);
    void setScale(qreal scale);
    qreal scale() const { return m_scale; }
    void resizeFlower(int width, int height);

protected:
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void wheelEvent(QWheelEvent* event) override;
    void contextMenuEvent(QContextMenuEvent* event) override;

private:
    qreal m_scale;
    QPoint m_dragPosition;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

private slots:
    void startCountdown();
    void updateCountdown();
    void saveToAlbum();
    void showAlbum();
    void chooseFlower();
    void chooseMusic();
    void showPlanResult();
    void inputPlan();
    void showCurrentPieChart();
    void openGarden();
    void addFlowerToGarden();
    void updateScale(int value);


private:
    Ui::MainWindow* ui;
    QTimer* timer;
    int remainingTime;
    int totalTime;
    QMap<QString, QVector<QPixmap>> flowerStages;
    QList<QPixmap> album;
    QString currentFlower;
    QMediaPlayer* mediaPlayer;
    QMediaPlaylist* playlist;
    QVector<PlanItem> planItems;
    QDialog* currentPieDialog;
    PieChartWidget* pieChartWidget;
    int currentStageIndex;


    QDialog* gardenDialog;
    QLabel* gardenBackground;
    QList<GardenItem*> gardenItems;
    QSlider* scaleSlider;
    QPushButton* addFlowerButton;


    void setupGarden();
    void addItemToGarden(const QPixmap& pixmap);
    void updateFlowerImage();
    void loadFlowerStages();
    void updatePlanProgress(int elapsed);
    void addItemToGarden(const QString& imagePath, const QPoint& position);
    void addItemToGarden(const QPixmap& pixmap, const QPoint& position);

};

#endif // MAINWINDOW_H
